#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main(void)
{
	int a=0,T = 0; int n = 0;
	scanf("%d", &T);
	for (a = 0; a < T; a++)
	{
		int arr[10][10];
		int A = 0, i = 0, m = 0;
		scanf("%d", &A);
		for (i = 0; i < A; i++)
			for (m = 0; m < A; m++)
				scanf("%d", &arr[i][m]);
		for (i = 1; i < A; i++)
			for (m = 0; m < A; m++)
			{
				if (m < i && arr[i][m] != 0)
				{
					printf("NO\n");
					goto loop;
				}
				else if (i == (A - 1) && m == (A - 2)&&arr[i][m]==0)
				{
					printf("YES\n");
					goto loop;
				}
			}
	loop:
		continue;
	}
	    
	return 0;
}